#include<iostream>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<functional>
#include<vector>
#include"task.h"

using namespace std;

typedef  function<void()> task_t;
//using task_t = function<void()>;

void LoadTask(vector<task_t>& tasks)
{
  tasks.push_back(PrintLog);
  tasks.push_back(DownLoad);
  tasks.push_back(Backup);
}


int main()
{
  vector<task_t> tasks;
  LoadTask(tasks);

  pid_t id=fork();
  if(id==0)
  {
    //child
    while(1)
    {
      printf("我是子进程，我的pid:%d\n",getpid());
      sleep(1);
    }
    exit(0);
  }
  else 
  {
    while(1)
    {
        sleep(1);
        pid_t rid=waitpid(id,nullptr,WNOHANG);
        if(rid>0)
        {
          printf("等待子进程:%d 成功\n",rid);
          break;
        }
        else if(rid<0)
        {
          printf("等待子进程失败\n");
          break;
        }
        else
        {
          printf("子进程尚未退出\n");
          
          //做自己的事情
          for(auto& task:tasks)
          {
            task();
          }
        }
    } 
  }
  return 0;
}
